package cn.com.headfree.job;

import java.util.ArrayList;
import java.util.List;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import cn.com.headfree.entity.TCommonTaskExecuteRecord;
import cn.com.headfree.service.IScheduleService;
import cn.com.headfree.service.ITaskService;

@Component
public class CommonScanJob {
	private static final Logger log = LogManager.getLogger(CommonScanJob.class);
	@Value("${quartz.scan}")
    private String scan;
	public static List<TCommonTaskExecuteRecord> recordList = new ArrayList<TCommonTaskExecuteRecord>();
	
	@Autowired
	private IScheduleService scheduleService;
	@Autowired
	private ITaskService taskService;
 
	@Scheduled(cron = "${quartz.time}")
	public void execute() {
		if (Boolean.parseBoolean(this.scan)) {
            this.scheduleService.proceedSchedule();
            this.writeRecord();
        }
	}
	
	/**
	 * @description 写任务操作记录
	 * @author fengya
	 * @date 2020年7月15日 下午5:20:35
	 * void
	 */
	private void writeRecord() {
		try {
			this.taskService.writeRecord(recordList);
			recordList.clear();
		} catch (Exception e) {
			log.error("保存任务执行记录失败！",e);
		}
	}
}
